package com.tools.s7.utils;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.tools.s7.R;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 自定义Toast
 */
public class PushToast {

    private static Toast mToast;
    private static final int SHOW = 1;
    private static final int HIDE = 0;
    private static Object mTN;
    private static Method mShow;
    private static Method mHide;
    private static Field mViewFeild;
    private static long durationTime = 3 * 1000;

    private View mNextView;

    public PushToast(Context context) {
        mToast = new Toast(context);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.setGravity(Gravity.TOP, 0, 100);
        reflectEnableClick();
        reflectToast();
    }

    public void show() {
        mToast.setView(mNextView);
        if (mShow != null && mHide != null) {
            handler.sendEmptyMessage(SHOW);
        } else {
            mToast.show();
        }
    }

    public static PushToast makeText(Context context, String content) {
        return makeText(context, null, content);
    }

    public static PushToast makeText(Context context, String title, String content) {
        PushToast push = new PushToast(context);
        LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflate.inflate(R.layout.toast_push, null);
        push.mNextView = view;
        TextView mTitle = view.findViewById(R.id.push_title);
        TextView mContent = view.findViewById(R.id.push_content);
        if (TextUtils.isEmpty(title)) {
            mTitle.setVisibility(View.GONE);
        } else {
            mTitle.setVisibility(View.VISIBLE);
            mTitle.setText(title);
        }
        mContent.setText(content);
        return push;
    }

    private static void reflectEnableClick() {
        try {
            Object mTN;
            mTN = getField(mToast, "mTN");
            if (mTN != null) {
                Object mParams = getField(mTN, "mParams");
                if (mParams != null && mParams instanceof WindowManager.LayoutParams) {
                    WindowManager.LayoutParams params = (WindowManager.LayoutParams) mParams;
                    //显示与隐藏动画
                    // params.windowAnimations = R.style.ClickToast;
                    // Toast可点击
//                    params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                    //设置viewgroup宽高
                    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
                    //设置Toast宽度为屏幕宽度
                    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    //设置高度
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射字段
     * @param object    要反射的对象
     * @param fieldName 要反射的字段名称
     */
    private static Object getField(Object object, String fieldName) throws NoSuchFieldException, IllegalAccessException {
        Field field = object.getClass().getDeclaredField(fieldName);
        if (field != null) {
            field.setAccessible(true);
            return field.get(object);
        }
        return null;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SHOW:
                    handler.sendEmptyMessageDelayed(HIDE, durationTime);
                    try {
                        //android4.0以上就要以下处理
                        if (Build.VERSION.SDK_INT > 14) {
                            Field mNextViewField = mTN.getClass().getDeclaredField("mNextView");
                            mNextViewField.setAccessible(true);
                            View v = mToast.getView();
                            mNextViewField.set(mTN, v);
                            Method method = mTN.getClass().getDeclaredMethod("show", null);
                            method.invoke(mTN, null);
                        }
                        mShow.invoke(mTN, null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case HIDE:
                    try {
                        mHide.invoke(mTN, null);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NullPointerException ex) {
                        ex.printStackTrace();
                    }
                    break;
            }
        }
    };

    public void reflectToast() {
        try {
            Field field = mToast.getClass().getDeclaredField("mTN");
            field.setAccessible(true);
            mTN = field.get(mToast);
            mShow = mTN.getClass().getDeclaredMethod("show");
            mHide = mTN.getClass().getDeclaredMethod("hide");
            mViewFeild = mTN.getClass().getDeclaredField("mNextView");
            mViewFeild.setAccessible(true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        }
    }

}